package gui;

import java.awt.Color;
import javax.swing.BorderFactory;
import ptolemy.plot.Plot;

import register.Register;

/**
 * Creates a plot of the 
 * @author Matt Cole
 *
 */
public class ProbPlot extends Plot {
	
	/** Number of states in the register */
	private int numStates;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Creates a plot of the probabilities of each state.
	 */
	public ProbPlot(int width) {
		
		// Format the plot area
		setColor(false);
		setSize(width, 500);
		setBackground(Color.white);
		setBorder(BorderFactory.createTitledBorder("Probabilities"));
		
	}
	
	/**
	 * Sets up the probabilities plot for the given register. 
	 * @param reg
	 */
	public void setup(Register reg) {
		numStates = reg.getNumStates();
		setBars(0.9,0);
		setXLabel("State");
		setYLabel("Probablilty of State");
		setXRange(-1, numStates);
		update(reg);
	}

	/**
	 * Adds the probabilities of each state of the given register to the plot.
	 * @param reg The register for which the probabilities are to be plotted.
	 */
	public void update(Register reg) {
		
		// Clear the dataset
		clear(2);
		
		// Add new points to the dataset
		for(int i = 0; i < numStates; i++) {
			addXTick(i+"", i);
			addPoint(2, i, reg.getProbabilityOfState(i), false);
		}
		
	}
	
	/**
	 * Clears all data from plot.
	 */
	public void reset() {
		clear(2);
		clear(true);
	}

}
